﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.HomeServer.Controls;
using RouterControl.IO;
using RouterControl.Whs.Wizards;
using Microsoft.HomeServer.Extensibility;
using System.Drawing;
using Microsoft.HomeServer.Common.Client;
using RouterControl.IO.UPnP;

namespace RouterControl.Diag
{
    public class WizardMain : Wizard
    {
        public enum Severity
        {
            Error,  // default
            Info,
            Warn,
            Success,
        }

        public struct ErrorInfo
        {
            public Severity Severity;
            public Exception Error;
            public string Title;
            public string Text;
            public Uri Link;

            public Color MessageColor
            {
                get
                {
                    switch (this.Severity)
                    {
                        case Severity.Error:
                            return CommonSettings.UnHealthyColor;

                        case Severity.Info:
                            return Color.Blue;

                        case Severity.Success:
                            return CommonSettings.HealthyColor;

                        case Severity.Warn:
                            return CommonSettings.AtRiskColor;

                        default:
                            throw new Exception("unknown severity");
                    }
                }
            }

            public Icon MessageIcon
            {
                get
                {
                    switch (this.Severity)
                    {
                        case Severity.Error:
                            return CommonImages.StatusCritical24Icon;

                        case Severity.Info:
                            return Properties.Resources.icoInfo;

                        case Severity.Success:
                            return CommonImages.StatusHealthy24Icon;

                        case Severity.Warn:
                            return CommonImages.StatusAtRisk24Icon;

                        default:
                            throw new Exception("unknown severity");
                    }
                }
            }
        }

        private List<ErrorInfo> _errors = new List<ErrorInfo>();

        public IList<ErrorInfo> Errors
        {
            get { return _errors; }
        }

        public IEnumerable<IUPnPDevice> FoundDevices
        {
            get;
            set;
        }

        public IUPnPDevice SelectedDevice
        {
            get;
            set;
        }

        public IConsoleServices Services
        {
            get;
            private set;
        }

        static WizardMain()
        {
            // install licenses
            Xceed.FileSystem.Licenser.LicenseKey = Microsoft.HomeServer.Common.Licensing.XceedZipLibraryLicenseKey;
            Xceed.Zip.Licenser.LicenseKey = Microsoft.HomeServer.Common.Licensing.XceedZipLibraryLicenseKeyEx;
        }

        public WizardMain(IConsoleServices services)
        {
            if (services == null)
	            throw new ArgumentNullException("services");

            this.Services = services;

            this.Text = string.Format(
                Properties.Resources.txtWizardTitle,
                RouterControlVersion.VersionString
                );

            this.Width = CommonSettings.BigWizardStdWidth;
            this.Height = CommonSettings.BigWizardStdHeight;
            this.WizardIcon = Properties.Resources.icoLogo;

            Init();

            this.SetTopLevel(true);
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetTypes(new[] {
                typeof(WizardPageHost<PageWelcome>),
                typeof(WizardPageHost<PagePrepare>),
                typeof(WizardPageHost<PageUpdateCheck>),
                typeof(WizardPageHost<PageSearch>),
                typeof(WizardPageHost<PageSelectRouter>),
                typeof(WizardPageHost<PageFeatureTest>),
                typeof(WizardPageHost<PageShowErrors>),
                });
        }
    }
}
